//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "HestonProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/StochasticProcess.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CHestonProcess::CHestonProcess (Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeRate, Cephei::QL::Termstructures::IYieldTermStructure^ dividendYield, Cephei::QL::IQuote^ s0, Double v0, Double kappa, Double theta, Double sigma, Double rho, Microsoft::FSharp::Core::FSharpOption<QL::Processes::HestonProcess::DiscretizationEnum>^ d) : CStochasticProcess(CHestonProcess::typeid)
{
    CYieldTermStructure^ _CriskFreeRate;
    CYieldTermStructure^ _CdividendYield;
    CQuote^ _Cs0;
    try
    {
#ifdef HANDLE
        _phHestonProcess = NULL;
#endif
        _CriskFreeRate = safe_cast<CYieldTermStructure^> (riskFreeRate);
        _CriskFreeRate->Lock();
        Handle<QuantLib::YieldTermStructure>& _riskFreeRate = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CriskFreeRate->GetHandle ()); 
        _CdividendYield = safe_cast<CYieldTermStructure^> (dividendYield);
        _CdividendYield->Lock();
        Handle<QuantLib::YieldTermStructure>& _dividendYield = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdividendYield->GetHandle ()); 
        _Cs0 = safe_cast<CQuote^> (s0);
        _Cs0->Lock();
        Handle<QuantLib::Quote>& _s0 = static_cast<Handle<QuantLib::Quote>&> (_Cs0->GetHandle ()); 
        QuantLib::Real _v0 = (QuantLib::Real)ValueHelper::Convert (v0); //d
        QuantLib::Real _kappa = (QuantLib::Real)ValueHelper::Convert (kappa); //d
        QuantLib::Real _theta = (QuantLib::Real)ValueHelper::Convert (theta); //d
        QuantLib::Real _sigma = (QuantLib::Real)ValueHelper::Convert (sigma); //d
        QuantLib::Real _rho = (QuantLib::Real)ValueHelper::Convert (rho); //d
        QuantLib::HestonProcess::Discretization _d = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Processes::HestonProcess::DiscretizationEnum>::IsSome::get (d) ? (QuantLib::HestonProcess::Discretization)d->Value : QuantLib::HestonProcess::Discretization::QuadraticExponentialMartingale); //10
        _ppHestonProcess = new boost::shared_ptr<QuantLib::HestonProcess> (new QuantLib::HestonProcess ( _riskFreeRate,  _dividendYield,  _s0,  _v0,  _kappa,  _theta,  _sigma,  _rho,  _d ));
        SetStochasticProcess (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHestonProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CriskFreeRate != nullptr) _CriskFreeRate->Unlock();
        if (_CdividendYield != nullptr) _CdividendYield->Unlock();
        if (_Cs0 != nullptr) _Cs0->Unlock();
    }
}
Cephei::QL::Processes::CHestonProcess::CHestonProcess (boost::shared_ptr<QuantLib::HestonProcess>& childNative, Object^ owner) : CStochasticProcess(CHestonProcess::typeid)
{
#ifdef HANDLE
	_phHestonProcess = NULL;
#endif
	_ppHestonProcess = &childNative;
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHestonProcess));
}
Cephei::QL::Processes::CHestonProcess::CHestonProcess (QuantLib::HestonProcess& childNative, Object^ owner) : CStochasticProcess(CHestonProcess::typeid)
{
#ifdef HANDLE
	_phHestonProcess = NULL;
#endif
	_ppHestonProcess = new boost::shared_ptr<QuantLib::HestonProcess> (&childNative);
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHestonProcess));
    _HestonProcessOwner = owner;
    _StochasticProcessOwner = owner;
}

Cephei::QL::Processes::CHestonProcess::CHestonProcess (CHestonProcess^ copy) : CStochasticProcess(CHestonProcess::typeid)
{
#ifdef HANDLE
	_phHestonProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHestonProcess = new boost::shared_ptr<QuantLib::HestonProcess> (copy->GetShared());
        _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHestonProcess));
    }
}
Cephei::QL::Processes::CHestonProcess::CHestonProcess (PLATFORM::Type^ t) : CStochasticProcess(CHestonProcess::typeid)
{
#ifdef HANDLE
	_phHestonProcess = NULL;
#endif
	if (!t->IsSubclassOf(CHestonProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CHestonProcess::CHestonProcess (QuantLib::Handle<QuantLib::HestonProcess>& childNative, Object^ owner)  : CStochasticProcess(CHestonProcess::typeid)
{
	_phHestonProcess = &childNative;
	_ppHestonProcess = &static_cast<boost::shared_ptr<QuantLib::HestonProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHestonProcess));
    _HestonProcessOwner = owner;
}
Cephei::QL::Processes::CHestonProcess::CHestonProcess (QuantLib::Handle<QuantLib::HestonProcess> childNative)  : CStochasticProcess(CHestonProcess::typeid)
{
	_phHestonProcess = &childNative;
	_ppHestonProcess = &static_cast<boost::shared_ptr<QuantLib::HestonProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHestonProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CHestonProcess::CHestonProcess (QuantLib::HestonProcess childNative)  : CStochasticProcess(CHestonProcess::typeid)
{
#ifdef HANDLE
	_phHestonProcess = NULL;
#endif
	_ppHestonProcess = new boost::shared_ptr<QuantLib::HestonProcess> (new QuantLib::HestonProcess (childNative));
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHestonProcess));
}
#endif

Cephei::QL::Processes::CHestonProcess::~CHestonProcess ()
{
    if (_ppHestonProcess != NULL)
    {
	    delete _ppHestonProcess;
        _ppHestonProcess = NULL;
    }
}
Cephei::QL::Processes::CHestonProcess::!CHestonProcess ()
{
    if (_ppHestonProcess != NULL)
    {
	    delete _ppHestonProcess;
    }
}
QuantLib::HestonProcess& Cephei::QL::Processes::CHestonProcess::GetReference ()
{
    if (_ppHestonProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppHestonProcess;
}
boost::shared_ptr<QuantLib::HestonProcess>& Cephei::QL::Processes::CHestonProcess::GetShared ()
{
    if (_ppHestonProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppHestonProcess;
}
QuantLib::HestonProcess* Cephei::QL::Processes::CHestonProcess::GetPointer ()
{
    if (_ppHestonProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppHestonProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HestonProcess>& Cephei::QL::Processes::CHestonProcess::GetHandle ()
{
	if (_phHestonProcess == NULL)
	{
		_phHestonProcess = new Handle<QuantLib::HestonProcess> (*_ppHestonProcess);
	}
	return *_phHestonProcess;
}
#endif
bool Cephei::QL::Processes::CHestonProcess::HasNative () 
{
	return (_ppHestonProcess != NULL);
}

Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Processes::CHestonProcess::DividendYield::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure>& _rv = (Handle<QuantLib::YieldTermStructure>&)(*_ppHestonProcess)->dividendYield ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CYieldTermStructure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHestonProcess::Kappa::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonProcess)->kappa ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHestonProcess::Rho::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonProcess)->rho ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Processes::CHestonProcess::RiskFreeRate::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure>& _rv = (Handle<QuantLib::YieldTermStructure>&)(*_ppHestonProcess)->riskFreeRate ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CYieldTermStructure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IQuote^ Cephei::QL::Processes::CHestonProcess::S0::get ()
{
    try
    {
    	Handle<QuantLib::Quote>& _rv = (Handle<QuantLib::Quote>&)(*_ppHestonProcess)->s0 ( );   
        Cephei::QL::CQuote^ _nrv = REFNEW Cephei::QL::CQuote (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHestonProcess::Sigma::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonProcess)->sigma ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Processes::CHestonProcess::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppHestonProcess)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHestonProcess::Theta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonProcess)->theta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHestonProcess::Time (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppHestonProcess)->time ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHestonProcess::V0::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonProcess)->v0 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Processes::IHestonProcess^ Cephei::QL::Processes::CHestonProcess_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeRate, Cephei::QL::Termstructures::IYieldTermStructure^ dividendYield, Cephei::QL::IQuote^ s0, Double v0, Double kappa, Double theta, Double sigma, Double rho, Microsoft::FSharp::Core::FSharpOption<QL::Processes::HestonProcess::DiscretizationEnum>^ d)
{
    return REFNEW CHestonProcess ( riskFreeRate,  dividendYield,  s0,  v0,  kappa,  theta,  sigma,  rho,  d);
}
